<template>
  <div>
    <el-card class="box-card" shadow="always">
      <div slot="header" class="clearfix">
        <b>
          <svg-icon icon-class="menu" />
          菜单列表</b>
      </div>
      <!-- 搜索表单 -->
      <el-form ref="queryForm" size="mini" :model="queryParams" :inline="true">
        <el-form-item>
          <el-button
            v-has-permission="['sys:menu:add']"
            type="success"
            icon="el-icon-plus"
            @click="handleAdd"
          >新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.name"
            placeholder="菜单名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="handleQuery"
          >搜索</el-button>
          <el-button
            icon="el-icon-refresh"
            @click="handleReset"
          >重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table
        v-loading="loading"
        :data="pageList"
        row-key="id"
        highlight-current-row
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        border
        size="mini"
        @row-click="handleRowClick"
      >
        <el-table-column label="菜单名称">
          <template slot-scope="scope">
            <svg-icon :icon-class="scope.row.icon ? scope.row.icon : 'build'" />
            {{ scope.row.name }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="130">
          <template slot-scope="scope">
            <el-button
              v-has-permission="['sys:menu:add']"
              type="success"
              icon="el-icon-plus"
              size="mini"
              circle
              plain
              @click.stop="handleAdd(scope.row)"
            />
            <el-button
              v-has-permission="['sys:menu:edit']"
              type="primary"
              icon="el-icon-edit"
              size="mini"
              circle
              plain
              @click.stop="handleEdit(scope.row)"
            />
            <el-button
              v-has-permission="['sys:menu:delete']"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              plain
              @click.stop="handleDelete(scope.row)"
            />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog :title="dialog.title" :visible.sync="dialog.visible" width="200">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="上级菜单" prop="parentId">
          <tree-select
            v-model="form.parentId"
            :options="menuOptions"
            placeholder="选择上级菜单"
            @select="handleMenuSelect"
          />
        </el-form-item>

        <el-form-item label="菜单名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入菜单名称" />
        </el-form-item>

        <el-form-item label="路由名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="system_user"
            style="width: 95%"
          />
          <el-tooltip
            effect="dark"
            content="vue-router编程式路由跳转方式之一，例：this.$router.push({name:'system_user',params:{id:1}})"
            placement="right"
          >
            <i
              class="el-icon-info"
              style="margin-left: 5px;color:darkseagreen"
            />
          </el-tooltip>
        </el-form-item>

        <el-form-item label="路由路径" prop="routePath">
          <el-input
            v-model="form.path"
            :placeholder="form.parentId == 0 ? '/system' : 'user'"
            style="width: 95%"
          />
          <el-tooltip
            effect="dark"
            content="vue-router编程式路由跳转方式之一，例：this.$router.push({path:'/system/user',query:{id:1}})"
            placement="right"
          >
            <i
              class="el-icon-info"
              style="margin-left: 5px;color:darkseagreen"
            />
          </el-tooltip>
        </el-form-item>

        <el-form-item label="组件路径" prop="component">
          <el-input
            v-model="form.component"
            :readonly="form.parentId == 0 ? true : false"
            placeholder="system/user/index"
          >
            <template
              v-if="form.parentId != 0"
              slot="prepend"
            >src/views/</template>
            <template v-if="form.parentId != 0" slot="append">.vue</template>
          </el-input>
        </el-form-item>

        <el-form-item label="菜单图标">
          <el-popover
            placement="bottom-start"
            width="460"
            trigger="click"
            @show="$refs['iconSelect'].reset()"
          >
            <IconSelect ref="iconSelect" @selected="handleIconSelected" />
            <el-input
              slot="reference"
              v-model="form.icon"
              placeholder="点击选择图标"
            >
              <svg-icon
                v-if="form.icon"
                slot="prefix"
                :icon-class="form.icon"
                class="el-input__icon"
                style="height: 40px;width: 16px; "
              />
              <i v-else slot="prefix" class="el-icon-search el-input__icon" />
            </el-input>
          </el-popover>
        </el-form-item>

        <el-form-item label="缓存" prop="keepAlive">
          <el-radio-group v-model="form.keepAlive">
            <el-radio :label="true">开启</el-radio>
            <el-radio :label="false">关闭</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="状态" prop="hidden">
          <el-radio-group v-model="form.hidden">
            <el-radio :label="false">显示</el-radio>
            <el-radio :label="true">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="固定面包屑" prop="affix">
          <el-radio-group v-model="form.affix">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="隐藏面包屑" prop="hideHeader">
          <el-radio-group v-model="form.hideHeader">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <template v-if='form.parentId == 0' >
          <el-form-item label="固定显示根菜单" prop="alwaysShow">
            <el-radio-group v-model="form.alwaysShow">
              <el-radio :label="true">是</el-radio>
              <el-radio :label="false">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </template>

        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            style="width: 100px"
            controls-position="right"
            :min="0"
          />
        </el-form-item>

        <el-form-item label="跳转路径">
          <el-input
            v-model="form.redirect"
            placeholder="请输入跳转路径"
            maxlength="50"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import TreeSelect from '@riophae/vue-treeselect'
import IconSelect from '@/components/IconSelect'
import { add, del, list, update, updateStatus } from '@/api/admin/menu'

export default {
  name: 'Menu',
  components: { TreeSelect, IconSelect },
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      queryParams: {
        name: undefined,
        queryMode: undefined
      },
      pagination: {
        page: 1,
        limit: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      form: {
        parentId: 0,
        hidden: false,
        icon: undefined,
        sort: 1,
        component: 'Layout',
        title: undefined,
        name: undefined,
        path: undefined,
        redirect: undefined,
        keepAlive: true,
        alwaysShow: false,
        affix: true,
        hideHeader: false
      },
      rules: {
        parentId: [
          { required: true, message: '请选择顶级菜单', trigger: 'blur' }
        ],
        name: [{ required: true, message: '请输入菜单名称', trigger: 'blur' }]
      },
      title: '新增菜单',
      menuOptions: [],
      currentRow: undefined
    }
  },
  created() {
    this.loadMenuOptions()
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.resetForm()
      this.queryParams.page = this.pagination.page
      this.queryParams.limit = this.pagination.limit
      this.queryParams.queryMode = 'list'
      list(this.queryParams).then(response => {
        this.pageList = response.data
        this.pagination.total = response.total
        this.loading = false
      })
    },
    handleReset() {
      this.pagination = {
        page: 1,
        limit: 10,
        total: 0
      }
      this.queryParams.name = undefined
      this.handleQuery()
      this.loadMenuOptions()
    },
    handleRowClick(row) {
      const currentRow = JSON.parse(JSON.stringify(row))
      this.currentRow = currentRow
      this.form = currentRow
      this.title = '修改菜单'
      this.$emit('menuClick', row)
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleStatusChange(row) {
      const text = row.status === 1 ? '启用' : '停用'
      this.$confirm('确认要"' + text + row.name + '"数据项吗?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return updateStatus(row.id, { status: row.status })
        })
        .then(() => {
          this.$message.success(text + '成功')
        }).finally(() => {
          this.handleQuery()
        })
    },

    handleAdd(row) {
      this.resetForm()
      this.dialog = {
        title: '新增菜单',
        visible: true
      }
      if (row.id) {
        // 行点击新增
        this.form.parentId = row.id
        this.form.component = undefined
      } else {
        // 工具栏点击新增
        if (this.currentRow) {
          this.form.parentId = this.currentRow.id
          this.form.component = undefined
        } else {
          this.form.parentId = 0
          this.form.component = 'Layout'
        }
      }
      this.title = '新增菜单'
    },
    handleEdit(row) {
      this.resetForm()
      this.dialog = {
        title: '修改菜单',
        visible: true
      }
      this.form = JSON.parse(JSON.stringify(row))
    },
    handleSubmit: function() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const id = this.form.id
          if (id !== undefined) {
            update(this.form).then(() => {
              this.$message.success('修改成功')
              this.handleQuery()
              this.loadMenuOptions()
              this.closeDialog()
            })
          } else {
            add(this.form).then(() => {
              this.$message.success('新增成功')
              this.handleQuery()
              this.loadMenuOptions()
              this.closeDialog()
            })
          }
        }
      })
    },
    handleDelete(row) {
      // const ids = [row.id || this.ids].join(',')
      const ids = row.id ? [row.id] : this.ids
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          del(ids).then(() => {
            this.$message.success('删除成功')
            this.handleQuery()
            this.loadMenuOptions()
          })
        })
        .catch(() => this.$message.info('已取消删除'))
    },
    resetForm() {
      this.form = {
        parentId: 0,
        hidden: false,
        icon: undefined,
        sort: 1,
        component: 'Layout',
        title: undefined,
        name: undefined,
        path: undefined,
        redirect: undefined,
        keepAlive: true,
        alwaysShow: false,
        affix: true,
        hideHeader: false
      }
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    },
    loadMenuOptions() {
      this.menuOptions = []
      list({ queryMode: 'tree' }).then(response => {
        const menuOption = { id: 0, label: '无', children: response.data }
        this.menuOptions.push(menuOption)
        this.$forceUpdate()
      })
    },
    handleIconSelected(name) {
      this.form.icon = name
      this.$forceUpdate()
    },
    handleMenuSelect(node) {
      if (node.id === 0) {
        this.form.component = 'Layout'
      } else {
        this.form.component = undefined
      }
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    }
  }
}
</script>

<style scoped></style>
